package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.OrderProductEntity;
import com.zhenwei.zscm.mapper.IOrderProductMapper;
import com.zhenwei.zscm.mappings.OrderProductMapping;
import com.zhenwei.zscm.model.dto.order.OrderProductDTO;
import com.zhenwei.zscm.model.enums.RepairStateEnum;
import com.zhenwei.zscm.model.vo.order.CreateOrderProductVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @author DuckyYang
* @description 针对表【order_product】的数据库操作Service实现
* @createDate 2025-05-22 09:40:52
*/
@Service
@AllArgsConstructor
public class OrderProductRepo extends ServiceImpl<IOrderProductMapper, OrderProductEntity> {
    private ShippingViewRepo shippingViewRepo;

    @Transactional
    public boolean createOrderProduct(String orderId, List<CreateOrderProductVO> vo) {
        List<OrderProductEntity> entityList = Linq.mapList(vo, OrderProductMapping.INSTANCE::vo2Entity);
        HashMap<String, Date> shippingDate = shippingViewRepo.getProductShippingDate(Linq.mapList(vo, CreateOrderProductVO::getProductSn));

        entityList.forEach(e -> {
            Long productRepairCount = getProductRepairCount(e.getProductSn());
            e.setRepairCount(Math.toIntExact(productRepairCount));
            e.setGuid(IdUtil.simpleUUID());
            e.setOrderId(orderId);
            e.setRepairState(RepairStateEnum.CHECKING.getText());
            e.setRepairStateCode(RepairStateEnum.CHECKING.name());
            Date date = shippingDate.get(e.getProductSn());
            e.setShippingDate(date);
        });
        return this.saveBatch(entityList);
    }
    public Long getProductRepairCount(String productSn) {
        var q = new LambdaQueryWrapper<OrderProductEntity>()
                .eq(OrderProductEntity::getProductSn, productSn)
                .eq(OrderProductEntity::getDeleted, false);
        return this.count(q);
    }
    public boolean deleteOrderProduct(String orderId) {
        var u = new LambdaUpdateWrapper<OrderProductEntity>()
                .eq(OrderProductEntity::getOrderId, orderId)
                .set(OrderProductEntity::getDeleted, true);
        MybatisWrapper.fillUser(u);
        return this.update(u);

    }

    public List<OrderProductDTO> getList(String orderId){
        var q = new LambdaQueryWrapper<OrderProductEntity>()
                .eq(OrderProductEntity::getOrderId, orderId)
                .eq(OrderProductEntity::getDeleted, false)
                .orderByAsc(OrderProductEntity::getId);
        List<OrderProductEntity> list = this.list(q);
        return Linq.mapList(list, OrderProductMapping.INSTANCE::entity2Dto);
    }

    public OrderProductDTO get(String orderId, String productSn) {
        var q = new LambdaQueryWrapper<OrderProductEntity>()
                .eq(OrderProductEntity::getOrderId, orderId)
                .eq(OrderProductEntity::getProductSn, productSn)
                .eq(OrderProductEntity::getDeleted, false);
        List<OrderProductEntity> list = this.list(q);
        return Linq.getFirst(list, OrderProductMapping.INSTANCE::entity2Dto);
    }

    public boolean updateState(String orderId, String productSn, String state){
        RepairStateEnum stateEnum = RepairStateEnum.valOf(state);

        var u = new LambdaUpdateWrapper<OrderProductEntity>()
                .eq(OrderProductEntity::getOrderId, orderId)
                .eq(OrderProductEntity::getProductSn, productSn)
                .eq(OrderProductEntity::getDeleted, false)
                .set(OrderProductEntity::getRepairStateCode, stateEnum.name())
                .set(OrderProductEntity::getRepairState, stateEnum.getText());
        return this.update(u);
    }
    public boolean updateState(String orderId, List<String> productSns, String state){
        RepairStateEnum stateEnum = RepairStateEnum.valOf(state);

        var u = new LambdaUpdateWrapper<OrderProductEntity>()
                .eq(OrderProductEntity::getOrderId, orderId)
                .in(OrderProductEntity::getProductSn, productSns)
                .eq(OrderProductEntity::getDeleted, false)
                .set(OrderProductEntity::getRepairStateCode, stateEnum.name())
                .set(OrderProductEntity::getRepairState, stateEnum.getText());
        return this.update(u);
    }

    public boolean checkAllProductRepaired(String orderId) {
        var q = new LambdaQueryWrapper<OrderProductEntity>()
                .eq(OrderProductEntity::getOrderId, orderId)
                .eq(OrderProductEntity::getDeleted, false)
                .ne(OrderProductEntity::getRepairStateCode, RepairStateEnum.REPAIRED.name())
                .ne(OrderProductEntity::getRepairStateCode, RepairStateEnum.CANCEL_REPAIR.name());
        return !this.exists(q);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<OrderProductDTO> orderProductList) {
        orderProductList.forEach(x->x.setGuid(IdUtil.simpleUUID()));
        List<OrderProductEntity> entityList = Linq.mapList(orderProductList, OrderProductMapping.INSTANCE::dto2Entity);
        return this.saveBatch(entityList);
    }

    public boolean delete(String orderId, String productSn) {
        var u = new LambdaUpdateWrapper<OrderProductEntity>()
                .eq(OrderProductEntity::getOrderId, orderId)
                .eq(OrderProductEntity::getProductSn, productSn)
                .eq(OrderProductEntity::getDeleted, false)
                .set(OrderProductEntity::getDeleted, true);
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }
}




